<?php
namespace Admin\Controller;
use Think\Controller;
class CategoryController extends CommonController {
    //节点分类
    public function indexAction(){
        $sql = "select count(id) count from auth_rule_category";

        $count = current(M()->query($sql))['count'];

        $Page  = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show();// 分页显示输出

        $catesql = "select * from auth_rule_category limit {$Page->firstRow},{$Page->listRows}";

        $cateres = M()->query($catesql);

        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->assign('cateres',$cateres);
        $this->display();
    }

    public function addAction() {
        if (IS_POST) {
            var_dump($_POST);
            $cname = $_POST['cname'];
            $cintroduce = $_POST['cintroduce'];
            $cid = $_POST['cid'];

            $catesql = "select cname from auth_rule_category where cname='{$cname}'";
            $cateres = M()->query($catesql);
            if ($cateres) {
                $this->error('该组已存在',U('admin/category/index'));
            }
             $cintroduce = $_POST['cintroduce'];
            if (empty($cintroduce)) {
                $this->error('添加失败，请填写内容',U('admin/category/index'));
            }
            $sql = "insert into auth_rule_category (cname,cintroduce) value ('{$cname}','{$cintroduce}')";

            if (M()->execute($sql)) {
                $this->success('添加成功',U('admin/category/index'));
            }else{
                $this->error('添加失败',U('admin/category/index'));
            }
        }
    }

    public function editAction() {
        if(IS_POST){
            // var_dump($_POST);
            $cname = $_POST['cname'];
            $cintroduce = $_POST['cintroduce'];
            $id = $_POST['id'];
            $sql = "update auth_rule_category set cname='{$cname}',cintroduce='{$cintroduce}' where id='{$id}'";
            if (M()->execute($sql)) {
                $this->success('修改成功',U('admin/category/index'));
            }else{
                $this->error('修改失败',U('admin/category/index'));
            }

        }else{
            $id = I('get.id','');
            if (empty($id)) {
                    $this->error('非法请求',U('admin/category/index'));
                }else{
                    $sql = "select * from auth_rule_category where id='{$id}'";
                    $res = current(M()->query($sql));
                    // var_dump($res);
                }
            }        
            $this->assign('res',$res);
            $this->display();
    }

    public function delAction() {
        $id = I('get.id');

        $sql = "delete from  auth_rule_category where id='{$id}'";

        $res = M()->execute($sql);

        if($res){
            $this->success('删除成功',U('admin/category/index'));
        }else{
            $this->error('删除失败',U('admin/category/index'));
        }
    }

}